package com.cyf.pc.service.impl;

import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.cyf.db.bean.ad.AdPayOrder;
import com.cyf.db.mapper.ad.AdPayOrderMapper;
import com.cyf.pay.alipay.service.AlipayService;
import com.cyf.pay.wxpay.bean.WxpayUnifiedOrderRsp;
import com.cyf.pay.wxpay.service.WxpayService;
import com.cyf.pc.network.bean.PCBusinessRsp;
import com.cyf.pc.network.bean.PCGetQrcodeReq;
import com.cyf.pc.network.bean.PCGetQrcodeRsp;
import com.cyf.pc.service.PCPayService;
import com.cyf.selfhelppay.network.bean.SHPayGetQrcodeReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * pc端支付相关接口
 * Created by hfy on 2017/5/16.
 */
@Service
public class PCPayServiceImpl implements PCPayService {

    private static final Logger logger = LoggerFactory.getLogger(PCPayServiceImpl.class);


    @Autowired
    private AlipayService alipayService;
    @Autowired
    private WxpayService wxpayService;

    @Autowired
    private AdPayOrderMapper adPayOrderMapper;

    @Override
    public PCGetQrcodeRsp getAdFeeQrcode(PCGetQrcodeReq req) {
        PCGetQrcodeRsp rsp = new PCGetQrcodeRsp();
        rsp.setResult(PCBusinessRsp.FAIL);
        try {
            AdPayOrder adPayOrder = adPayOrderMapper.getByOrderNo(req.getOrderNo());
            if (adPayOrder == null) {
                rsp.setStrError("订单不存在！");
                return rsp;
            }
            if (adPayOrder.getStatus() == AdPayOrder.STATUS_CLOSE) {
                rsp.setStrError("订单已关闭！");
                return rsp;
            } else if (adPayOrder.getStatus() != AdPayOrder.STATUS_WAIT_PAY) {
                rsp.setStrError("订单已支付！");
                return rsp;
            }
            // 获取二维码码串
            if (req.getPayWay() == SHPayGetQrcodeReq.PAYWAY_ALIPAY) {
                AlipayTradePrecreateResponse alipayTradePrecreateResponse = alipayService.getQrcode(adPayOrder.getOrderNo(), (adPayOrder.getTxnAmt() * 0.01) + "", "支付广告费");
                if (alipayTradePrecreateResponse == null || !alipayTradePrecreateResponse.isSuccess()) {
                    rsp.setStrError("获取支付宝支付二维码失败！");
                    return rsp;
                }
                rsp.setQrcode(alipayTradePrecreateResponse.getQrCode());
                adPayOrderMapper.updatePayWay(AdPayOrder.TRADETYPE_PC, AdPayOrder.PAYWAY_ALIPAY, adPayOrder.getId());
            } else if (req.getPayWay() == SHPayGetQrcodeReq.PAYWAY_WECHAT) {
                WxpayUnifiedOrderRsp wxpayUnifiedOrderRsp = wxpayService.getQrcode(adPayOrder.getOrderNo(), adPayOrder.getTxnAmt(), req.getReqIp(), "支付广告费");
                if (WxpayUnifiedOrderRsp.FAIL.equals(wxpayUnifiedOrderRsp.getReturn_code()) || WxpayUnifiedOrderRsp.FAIL.equals(wxpayUnifiedOrderRsp.getResult_code())) {
                    rsp.setStrError("获取微信支付二维码失败！");
                    return rsp;
                }
                rsp.setQrcode(wxpayUnifiedOrderRsp.getCode_url());
                adPayOrderMapper.updatePayWay(AdPayOrder.TRADETYPE_PC, AdPayOrder.PAYWAY_WECHAT, adPayOrder.getId());
            } else {
                rsp.setStrError("不支持此种支付方式！");
                return rsp;
            }
//            rsp.setQrcode("https://qr.alipay.com/bax09477qwq0gl76rpkq20d0");
            rsp.setResult(PCBusinessRsp.SUCCESS);
        } catch (Exception e) {
            logger.error("pc端获取二维码码串异常：{}", e);
            rsp.setStrError("服务器异常！");
        }
        return rsp;
    }
}
